package com.itcast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itcast.entity.PageResult;
import com.itcast.entity.QueryPageBean;
import com.itcast.mapper.CheckGroupMapper;
import com.itcast.pojo.CheckGroup;
import com.itcast.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
    private CheckGroupMapper checkGroupMapper;

    //添加检查组
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //操作t_checkgroups表
        checkGroupMapper.add(checkGroup);
        //操作多表t_checkgitem_checkgroup
        //获取主键id
        Integer checkgroupId = checkGroup.getId();
        /*for (Integer checkitemId : checkitemIds) {
            checkGroupMapper.addOne(checkgroupId,checkitemId);
        }*/
        //调用多表查询关系
        this.setCheckGroupAndItem(checkgroupId,checkitemIds);
    }
    //分页查询
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();//页码
        Integer pageSize = queryPageBean.getPageSize();//每页记录数
        String queryString = queryPageBean.getQueryString();//查询条件
        PageHelper.startPage(currentPage,pageSize);
        //调用dao根据条件查询数据
        Page<CheckGroup> page = checkGroupMapper.findByPage(queryString);

        return new PageResult(page.getTotal(),page.getResult());
    }
    //根据id查询检查组
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupMapper.findById(id);
    }

    //根据id回显多个检查项
    @Override
    public List<Integer> findCheckItemByGroup(Integer id) {

        List<Integer> checkItemId = checkGroupMapper.findCheckItemByGroup(id);
        //System.out.println(checkItemId);
        return checkItemId;
    }
    //编辑检查组和检查项
    @Override
    public void edit(CheckGroup checkGroup, Integer[] checkitemIds) {
        //修改检查组基本信息，操作检查组t_checkgroup表
        checkGroupMapper.edit(checkGroup);
        //清理中间表，操作t_checkgroup_checkitem表
        checkGroupMapper.deleteAssocication(checkGroup.getId());
        //重新建立检查组和检查项的中间表
        Integer checkGroupId = checkGroup.getId();
        this.setCheckGroupAndItem(checkGroupId,checkitemIds);

    }
    //删除检查组
    @Override
    public void deleteById(Integer checkgroupId) {
        //根据id查询关系表t_checkgroup_checkitem是否有数据
        Integer count = checkGroupMapper.findByIdCheckGroupItem(checkgroupId);
        //根据id查询关系表t_setmeal_checkgroup是否有数据
        //   此方法待写
        if (count > 0){
            //不能删除
            throw new RuntimeException();
        }
        //删除检查组
        checkGroupMapper.deleteById(checkgroupId);
    }
    //查询所有
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupMapper.findAll();
    }

    //建立检查组和检查项多对多关系
    public void setCheckGroupAndItem(Integer checkgroupId, Integer[] checkitemIds){
        if (checkitemIds!=null && checkitemIds.length>0){
            for (Integer checkitemId : checkitemIds) {
                Map<String,Integer> map = new HashMap<>();
                map.put("checkgroupId",checkgroupId);
                map.put("checkitemId",checkitemId);
                checkGroupMapper.setCheckItemAndGroup(map);
            }
        }
    }
}
